package com.dsbj.icardservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.icardservice.entity.Reservation;
import com.dsbj.icardservice.entity.ReservationPrimary;
import com.dsbj.icardservice.mapper.ReservationMapper;
import com.dsbj.icardservice.mapper.ReservationPrimaryMapper;
import com.dsbj.icardservice.utils.strprocess.Base64ImageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cjw 2019-10-09
 */
@Service
@Transactional
public class ReservationPrimaryService {

	@Resource
	private ReservationPrimaryMapper reservationPrimaryMapper;
	
	@Resource
	private ReservationMapper reservationMapper;
	
	/**
	 * 根据createUserEmployeeno分页获取预约单列表。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	public Page<ReservationPrimary> getReservationPrimaryByPage(String createUserEmployeeno,int pagesize,int pagenum){
		
		Page<ReservationPrimary> page=new Page<ReservationPrimary>().setSize(pagesize).setCurrent(pagenum);
		QueryWrapper<ReservationPrimary> queryWrapper=new QueryWrapper<ReservationPrimary>().select("id","RESERVATIONNO","EMPLOYEENO_RECEPTION","EMPLOYEENAME_RECEPTION","FORECAST_VISITING_TIME","VISITING_MATTERS","CREATE_USER_EMPLOYEENO")
				.eq("CREATE_USER_EMPLOYEENO",createUserEmployeeno)
				.orderByDesc("CREATE_TIME");
		reservationPrimaryMapper.selectPage(page,queryWrapper);
		List<ReservationPrimary> reservationPrimaries=page.getRecords();
		if(!CollectionUtils.isEmpty(reservationPrimaries)){
			List<Reservation> reservations= reservationMapper.selectList(
					new QueryWrapper<Reservation>()
							.in("RESERVATIONNO",reservationPrimaries.stream().map(ReservationPrimary::getReservationno).collect(Collectors.toList()))
							.orderByAsc("CREATE_TIME"));
			Map<String,List<Reservation>> groupMap = reservations.stream().collect(Collectors.groupingBy(Reservation::getReservationno));
			reservationPrimaries.forEach(x->x.setVisitorNames(groupMap.get(x.getReservationno()).stream().map(Reservation::getVisitor).collect(Collectors.joining(","))));
		}
		return page;
	}
	
	/**
	 * 将list_reservation存入reservationPrimary中。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	public ReservationPrimary supplementReservationPrimaryWithReservation(ReservationPrimary reservationPrimary){
		if(reservationPrimary==null){
			return null;
		}
		List<Reservation> reservations= reservationMapper.selectList(
				new QueryWrapper<Reservation>()
						.eq("RESERVATIONNO",reservationPrimary.getReservationno())
						.orderByAsc("CREATE_TIME"));
		reservations.parallelStream().forEach(x->{
			if(StringUtils.isNoneBlank(x.getIdcardnoUrl())){
				String idcardnoStr_base64= Base64ImageUtil.getFileStrBase64_fromDownloadUrl(x.getIdcardnoUrl());
				x.setPhotoBase64(idcardnoStr_base64);
			}
		});
		reservationPrimary.setReservationList(reservations);
		reservationPrimary.setVisitorNames(reservations.stream().map(x->x.getVisitor()).collect(Collectors.joining(",")));
		return reservationPrimary;
	}

	/**
	 * 根据reservationPrimaryId获取访客详情。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	public ReservationPrimary getReservationPrimaryById(String reservationPrimaryId){
		ReservationPrimary reservationPrimary=reservationPrimaryMapper.selectById(reservationPrimaryId);
		//将list_reservation存入reservationPrimary中
		supplementReservationPrimaryWithReservation(reservationPrimary);
		return reservationPrimary;
	}
	
	
	












	
}
